package org.loong.crypto.core;

import org.bouncycastle.asn1.ASN1ObjectIdentifier;

/**
 * The base class for algorithm names.
 */
public class Algorithm {

    /**
     * No algorithm.
     */
    public static final Algorithm NONE = new Algorithm("none");

    /**
     * The algorithm name.
     */
    private final String name;

    /**
     * The algorithm object identifier.
     */
    private ASN1ObjectIdentifier oid;

    /**
     * Creates a new crypto algorithm name.
     *
     * @param name The algorithm name. Must not be {@code null}.
     */
    public Algorithm(final String name) {
        this(name, null);
    }

    /**
     * Creates a new crypto algorithm name.
     *
     * @param name The algorithm name. Must not be {@code null}.
     * @param oid the algorithm object identifier. Must not be {@code null}.
     */
    public Algorithm(final String name, final ASN1ObjectIdentifier oid) {
        if (name == null) {
            throw new IllegalArgumentException("The algorithm name must not be null");
        }

        this.name = name;
        this.oid = oid;
    }

    /**
     * Gets the name of this algorithm.
     *
     * @return The algorithm name.
     */
    public final String getName() {
        return name;
    }

    /**
     * Gets algorithm object identifier.
     *
     * @return the algorithm object identifier
     */
    public ASN1ObjectIdentifier getOID() {
        return oid;
    }

    /**
     * Overrides {@code Object.hashCode()}.
     *
     * @return The object hash code.
     */
    @Override
    public final int hashCode() {
        return name.hashCode();
    }

    /**
     * Overrides {@code Object.equals()}.
     *
     * @param object The object to compare to.
     * @return {@code true} if the objects have the same value, otherwise {@code false}.
     */
    @Override
    public boolean equals(final Object object) {
        return object instanceof Algorithm && this.toString().equals(object.toString());
    }

    /**
     * Returns the string representation of this algorithm.
     *
     * @return The string representation.
     */
    @Override
    public final String toString() {
        return name;
    }
}
